package com.andy.qhb.controller.game;

import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.andy.qhb.base.RespVO;
import com.andy.qhb.controller.GameController;
import com.andy.qhb.dao.DaoUtils;
import com.andy.qhb.dao.vo.Where;
import com.andy.qhb.entity.UsrFriends;
import com.andy.qhb.entity.UsrMsgLog;
import com.andy.qhb.game.GameUtils;
import com.andy.qhb.game.GameUser;
import com.andy.qhb.game.GlobalHttp;
import com.andy.qhb.utils.JsonUtils;

@Controller
@RequestMapping(value="game/api")
public class Api116Controller extends GameController{
	
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	/**
	 * 确认好友邀请
	 * @param session
	 * @param data
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value="116")
	public RespVO action(HttpSession session,String data) throws Exception{
		GameUser user = getUser(session);
		Map<String, Object> reqData = JsonUtils.json2Map(data);
		
		Long msgId = MapUtils.getLong(reqData,"msgId");
		UsrMsgLog msgLog = user.getRlog(msgId);
		if(msgLog == null) return RespVO.createErrorJsonResonse("消息不存在");
		msgLog.setStatus(1);
		DaoUtils.update(msgLog);
		
		GameUser tarUser = GameUtils.getUsrUser(msgLog.getUid());
		user.getFriends().addFriend(tarUser.getId(), tarUser.getNickName(),tarUser.getIcon());
		DaoUtils.update(user.getFriends());
		
		GameUser user2 = GlobalHttp.getUser(tarUser.getId());
		if(user2 != null){
			user2.getFriends().addFriend(user.getId(), user.getNickName(),user.getUser().getIcon());
			DaoUtils.update(user2.getFriends());
		}else{
			// 申请者走sql
			UsrFriends tarFriends = DaoUtils.getOne(UsrFriends.class, Where.eq("uid", tarUser.getId()));
			if(tarFriends != null){
				tarFriends.setFids(tarFriends.getFids()+user.getId()+",");
				DaoUtils.update(tarFriends);
			}
		}
		
		
		return RespVO.createSuccessJsonResonse(msgLog.getUidOther());
		
	}
	
	
}